//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "math\fn_getShape.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

// #define TMF_SHAPE_CENTER 0
// #define TMF_SHAPE_WIDTH 1
// #define TMF_SHAPE_HEIGHT 2
// #define TMF_SHAPE_ANGLE 3
// #define TMF_SHAPE_RECTANGLE 4

private ["_shape","_input","_count","_center","_width","_height","_angle","_rectangle"];
DEFAULT_PARAM(_input,0,[]);
_count = count _input;
_shape = [];
SETVAL(_shape,TMF_SHAPE_CENTER,ARR_3(0,0,0));
SETVAL(_shape,TMF_SHAPE_WIDTH,0);
SETVAL(_shape,TMF_SHAPE_HEIGHT,0);
SETVAL(_shape,TMF_SHAPE_ANGLE,0);
SETVAL(_shape,TMF_SHAPE_RECTANGLE,false);

switch (_count) do {
	case 1: {
		_center = GETVAL(_input,0,ARR_3(0,0,0));
		if (AND_ALSO(IS_ARRAY(_center),count _center == 3)) then {
			SETVAL(_shape,TMF_SHAPE_CENTER,_center);
		};
	};
	case 2: {
		// [[1,2,3],20]
		_center = GETVAL(_input,0,ARR_3(0,0,0));
		_width = GETVAL(_input,1,0);
		if (AND_ALSO(IS_ARRAY(_center),count _center == 3)) then {
			SETVAL(_shape,TMF_SHAPE_CENTER,_center);
		};
		if (AND_ALSO(IS_SCALAR(_width),_width > 0)) then {
			SETVAL(_shape,TMF_SHAPE_WIDTH,_width);
			SETVAL(_shape,TMF_SHAPE_HEIGHT,_width);
		};
	};
	case 3: {
		// [[1,2,3],20,90]
		// [[1,2,3],[20,40],90]
		// [[1,2,3],20,false]
		// [[1,2,3],[20,40],false]
		_center = GETVAL(_input,0,ARR_3(0,0,0));
		_width = GETVAL(_input,1,0);
		_angle = GETVAL(_input,2,0);
		_height = 0;
		if (AND_ALSO(IS_ARRAY(_center),count _center == 3)) then {
			SETVAL(_shape,TMF_SHAPE_CENTER,_center);
		};
		if (IS_ARRAY(_width)) then {
			_height = GETVAL(_width,1,0);
			_width = GETVAL(_width,0,0);
		};
		if (AND_ALSO(IS_SCALAR(_width),_width > 0)) then {
			SETVAL(_shape,TMF_SHAPE_WIDTH,_width);
			if (AND_ALSO(IS_SCALAR(_height),_height > 0)) then {
				SETVAL(_shape,TMF_SHAPE_HEIGHT,_height);
			} else {
				SETVAL(_shape,TMF_SHAPE_HEIGHT,_width);
			};
		};
		if (IS_BOOL(_angle)) then {
			SETVAL(_shape,TMF_SHAPE_RECTANGLE,_angle);
		} else {
			if (AND_ALSO(IS_SCALAR(_angle),_angle > 0)) then {
				SETVAL(_shape,TMF_SHAPE_ANGLE,_angle);
			};
		};
	};
	case 4: {
		// [[1,2,3],20,40,90]
		// [[1,2,3],20,90,false]
		// [[1,2,3],[20,40],90,false]
		_center = GETVAL(_input,0,ARR_3(0,0,0));
		_width = GETVAL(_input,1,0);
		_angle = GETVAL(_input,2,0);
		_rectangle = GETVAL(_input,3,false);
		_height = 0;
		if (AND_ALSO(IS_ARRAY(_center),count _center == 3)) then {
			SETVAL(_shape,TMF_SHAPE_CENTER,_center);
		};
		if (IS_ARRAY(_width)) then {
			_height = GETVAL(_width,1,0);
			_width = GETVAL(_width,0,0);
		} else {
			if (!IS_BOOL(_rectangle)) then {
				_height = _angle;
				_angle = _rectangle;
				_rectangle = false;
			};
		};
		if (!IS_SCALAR(_angle)) then {
			_angle = 0;
		};
		if (!IS_BOOL(_rectangle)) then {
			_rectangle = false;
		};
		if (AND_ALSO(IS_SCALAR(_width),_width > 0)) then {
			SETVAL(_shape,TMF_SHAPE_WIDTH,_width);
			if (AND_ALSO(IS_SCALAR(_height),_height > 0)) then {
				SETVAL(_shape,TMF_SHAPE_HEIGHT,_height);
			} else {
				SETVAL(_shape,TMF_SHAPE_HEIGHT,_width);
			};
		};
		SETVAL(_shape,TMF_SHAPE_ANGLE,_angle);
		SETVAL(_shape,TMF_SHAPE_RECTANGLE,_rectangle);
	};
};

TRACE_1("End",_shape);
TMFPROFILERSTOP;
_shape;